from decimal import Decimal
from faker import Faker

INSERT_SQL = 'INSERT INTO books (book_id, category, title, author, pub_date, price, amount, star, cover) ' +\
    'values ({book_id}, {category}, \'{title}\', \'{author}\', to_date(\'{pub_date}\', \'YYYYMMDD\'), {price}, {amout},' +\
    '{star}, {cover});'


def write(n):
    f1 = Faker()
    f2 = Faker('zh_CN')

    with open('a.sql', mode='w', encoding='utf8') as f:
        for i in range(0, n):
            amount = f1.random_int(-5, 15)
            if amount < 0:
                amount = 0
            if amount != 0 and amount % 4 == 0:
                amount = amount + 1000

            if i % 2 == 0:
                title = f1.word() + f1.word() + f1.word()
                author = f1.name()
            else:
                title = f2.word() + f2.word() + f2.word()
                author = f2.name()
            if i % 3 == 0:
                title = title[:1] + '%' + title[1:]
            elif i % 5 == 0:
                title = title + '#'
            elif i % 4 == 0:
                title = '$' + title

            price = f1.pydecimal(right_digits=1, min_value=3, max_value=999) / 10
            if i % 4 == 0:
                price = price + 2000

            print(INSERT_SQL
                  .format(
                      book_id=i+1,
                      category=f1.random_int(1, 3),
                      title=title,
                      author=author,
                      pub_date=f1.past_date(start_date='-1000d').strftime('%Y%m%d'),
                      price=price,
                      amout=amount,
                      star=1 if f1.random_int(1, 3) == 1 else 0,
                      cover='NULL',
                  ), end='\n', file=f)


if __name__ == '__main__':
    write(512)
